﻿#include "answeritem.h"
#include <QDebug>


//answeritem& answeritem::operator=(const answeritem&ai)
//{
//    this->m_label=ai.m_label;
//    this->m_answers=ai.m_answers;
//    this->m_num=ai.m_num;
//    for(int i=0;i<ai.m_items.size();i++)
//    {
//        this->add_item(ai.m_items[i].first);
//    }
//    qDebug()<<"调用answeritem::operator="<<endl;
//    return *this;
//}

//answeritem::answeritem()
//{
//    qDebug()<<"调用answeritem::answeritem()"<<endl;
//    this->m_num=0;
//}

//answeritem::answeritem(const answeritem&ai)
//{
//    this->m_label=ai.m_label;
//    this->m_answers=ai.m_answers;
//    this->m_num=0;
//    for(int i=0;i<ai.m_items.size();i++)
//    {
//        this->add_item(ai.m_items[i].first);
//    }
//    qDebug()<<"调用answeritem(const answeritem&ai)"<<endl;

//}

answeritem::answeritem(const QString Label,QStringList answers)//一般答案不允许修改,所以放在构造函数要好一些.最好做一些出厂的设置
{
    this->m_label=Label;
    this->m_answers=answers;
    this->m_num=0;

    m_signle_type=true;
    if(answers.size()>1)//不让外部设置,直接根据答案来设置
    {
        m_signle_type=false;
    }
}

answeritem::~answeritem()
{

}

unsigned int answeritem::get_num()const
{
    return m_num;
}

bool answeritem::add_item(QString item)//"A","B"C,正确,错误
{
    for(int i=0;i<m_items.count();i++)
    {
        if(m_items[i].first==item)//如果选项已经有了,就不添加,如AA
        {
            return false;
        }
    }
    m_items.append(QPair<QString,int>(item,0));
    m_num++;
    return true;
}

QVector<QPair<QString,int>>& answeritem::get_items ()
{
    return m_items;
}

bool answeritem::valid_item()const//是否正确答案.
{
    int checked_num=0;
    for(int i=0;i<m_items.size();i++)
    {
        if(m_items[i].second==1)
        {
           if(m_answers.contains(m_items[i].first))//多选题的话,CA和AC都一样,所以不能按顺序来匹配
            {
                checked_num++;//统计勾选的答案数量
            }
           else
           {
               return false;
           }

        }
    }

    if(checked_num!=m_answers.size())//如果勾选的与答案的数量不一样,就错误
    {
        return false;
    }

    return true;
}

//void answeritem::set_total_score(int score)
//{
//    this->m_total_score=score;//设置题目的分值
//}


//int answeritem::get_score(void)const
//{
//    if(valid_item())//正确才返回分否则0分
//    {
//        return m_total_score;
//    }
//    return 0;
//}


QString answeritem::get_label() const
{
    return m_label;
}
